%%% ---------------------------------------------------------------
%%% objpats - part of ECT, an Object-oriented extension to Erlang
%%% Written by Gábor Fehér, 2008 at BUTE
%%% ---------------------------------------------------------------
%%% Relaxates class-manipulation patterns and expressions into
%%% element, setelement and size BIF-calls.
%%% ---------------------------------------------------------------
%%%


%% the status of processing a pattern, see flat_pattern in module flat

%%---------------------------------------------------------------------
%% Data Type: relaxstate
%% where:
%%    expr_path: AST expression
%%    list:     [ #flatitem{} ]
%%    mode:     'normal'| 'shallow'
%%    records:  [ {atom(), [atom()]} ]
%%    classes:  
%%----------------------------------------------------------------------
-record(relaxstate,
    {expr_path,      %% the current path in the pattern while parsing, this is copied to flatitems
     list,           %% the list of created #flatitems{}
     mode,           %% normal/shallow
     records,        %% list of record definitions see module recrods
     classes,        %% information about visible classes in the source code
     cx_class        %% the class in which the current pattern is implemented, it is set to [] if the pattern is in a module (needed for field visibility checking)
	}).

%% a single item 
-record(flatitem,
    {type,      %% eq, guard
     arg,       %% the variable or constant on the left of this item
     expr       %% the path of this flatitem
    }).
%% example
%% pattern: X = #xrecord{a = A, b = {C, atom}} translates to 
%% a list: [
%%          #flatitem{arg = xrecrod, path = [{tuple_item, 1}, {varname, 'X'}]},   %% record type match
%%          #flatitem{arg = A      , path = [{tuple_item, 2}, {varname, 'X'}]},   %% first field match
%%          #flatitem{arg = C      , path = [{tuple_item, 3}, {tuple_item, 1}, {varname, 'X'}]},  %% second field, first tuple item
%%          #flatitem{arg = atom   , path = [{tuple_item, 3}, {tuple_item, 2}, {varname, 'X'}]}   %% second field, second tuple item
%%         ]
%% The basic idea is that the variables and constants in the pattern can be expressed from X, the left hand variable.
%% The steps of this expression are in the list of path, and the the variable/constant itself is in the field arg.
%% The elements of the path are interpreted left-to-right. The rightmost element defines the name of the original variable.
%% After this, each element defines an operation, which takes that variable, and gives a variable to the next operation. That
%% last result has a relation of equality with arg, with the exception of {min_size}
%% 
%% These list elements can be easily translated to element(N, Tuple) based recursive expressions,
%% and these expressions will be equivalently perform the pattern matchnig. E.g.:
%%   atom = element(2, element(3, X))
%%
%% Possible items:
%%    {varname, 'X'}
%%    {head}
%%    {tail}
%%    {tuple_item, ItemNum}
%% the following operators must be rightmost in the list
%%    {tuple_size}  % the result is a number, and is must be equal to arg
%%    {min_size}    % the reulst is a number, and is must be greater than, or equal to arg
